import { reactive, ref, computed } from 'vue';
import { defineStore } from 'pinia';
import StorageService from '@/services/storage';
import { LOCALSTORAGE_TOKEN, LOCALSTORAGE_PERMISSIONS } from '@/config';

export const useAuthStore = defineStore('auth', () => {
  const token = ref(StorageService.getLocal(LOCALSTORAGE_TOKEN, ''));
  const permissions = ref(StorageService.getLocal(LOCALSTORAGE_PERMISSIONS, []));
  const permissionsMap = computed(() => {
    return permissions.value.reduce((a: any, b: string | number) => {
      a[b] = true;
      return a;
    }, {});
  });

  function setToken(value: string) {
    token.value = value;
  }
  function setPermissions(value: any[]) {
    permissions.value = value || [];
  }
  return { token, permissions, permissionsMap, setToken, setPermissions };
});
